//
//  THHomeDataController.m
//  TreeHole
//
//  Created by apple on 2020/7/29.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THHomeDataController.h"

@implementation THHomeDataController
//首页
+ (void)stoneobs_fun_get_home_model:(void(^)(THHomeModel * model))handle{
    dispatch_group_t group = dispatch_group_create();
    THHomeModel * mineModel = THHomeModel.new;
    dispatch_group_enter(group);
    [self stoneobs_fun_get_banner_request_cid:@"1" num:10 handle:^(NSArray * _Nonnull banner) {
        mineModel.banner = banner;
        dispatch_group_leave(group);
    }];
    

    dispatch_group_enter(group);
    [self stoneobs_fun_get_home_cates:^(NSArray * _Nonnull cates) {
        NSMutableArray * dealArray = cates.mutableCopy;
        ({
            THCateModel * model = THCateModel.new;
            model.c_id = @"";
            model.name = @"秒杀星期六";
            model.local_icon_name = @"秒杀星期六";
            [dealArray addObject:model];
        });
        ({
            THCateModel * model = THCateModel.new;
            model.c_id = @"";
            model.name = @"身边育婴师";
            model.local_icon_name = @"身边育婴师";
            [dealArray addObject:model];
        });

        mineModel.cates = dealArray;
        dispatch_group_leave(group);
    }];
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (handle) {
            handle(mineModel);
        }
    });
}
//获取banner
+ (void)stoneobs_fun_get_banner_request_cid:(NSString*)cid
                                        num:(NSInteger)num
                                     handle:(void(^)(NSArray * banner))handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (cid.length) {
        [stoneobs_net_param_dic setObject:cid forKey:@"cid"];
    }
    if (num>0) {
        [stoneobs_net_param_dic setObject:@(num) forKey:@"num"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/base/getBanner"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSArray * stoneobs_net_array = [responseObject valueForKey:@"data"];
        NSArray * banner = [THBannerModel mj_objectArrayWithKeyValuesArray:stoneobs_net_array];
        if (handle) {
            handle(banner);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        if (handle) {
            handle(@[]);
        }
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
//首页分类
+ (void)stoneobs_fun_get_home_cates:(void(^)(NSArray * cates))handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    //v1/goods/getAllCate 所有分类
    //v1/goods/getTop"  首页分类
//    v1/goods/getTop
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/getTop"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSArray * stoneobs_net_array = [responseObject valueForKey:@"data"];
        NSArray * cates = [THCateModel mj_objectArrayWithKeyValuesArray:stoneobs_net_array];
        if (handle) {
            handle(cates);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        if (handle) {
            handle(@[]);
        }
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}

#pragma mark --商品详情
+ (void)stoneobs_fun_get_good_detail:(NSString*)good_id handle:(void(^)(THGoodDetailModel * model))handle{

    dispatch_group_t group = dispatch_group_create();
    __block THGoodDetailModel * dmodel = THGoodDetailModel.new;
    __block NSMutableArray * comment = NSMutableArray.new;
    
    dispatch_group_enter(group);
    [self stoneobs_fun_get_only_good_detail:good_id handle:^(THGoodDetailModel *model) {
         dmodel = model;
         dispatch_group_leave(group);
    }];
    dispatch_group_enter(group);
    [self stoneobs_fun_get_good_comment_request_cid:good_id page:1 handle:^(NSArray<THCommentModel *> * _Nonnull array) {
        comment = array.mutableCopy;
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        dmodel.cus_comment = comment;
        if (handle) {
            handle(dmodel);
        }
    });
}
+ (void)stoneobs_fun_get_only_good_detail:(NSString*)good_id handle:(void(^)(THGoodDetailModel * model))handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (good_id.length) {
        [stoneobs_net_param_dic setObject:good_id forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/detail"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {

        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        THGoodDetailModel * model = [THGoodDetailModel mj_objectWithKeyValues:stoneobs_net_model_dic];
        if (handle) {
            handle(model);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        if (handle) {
            handle(THGoodDetailModel.new);
        }
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
//获取商品评论
+ (void)stoneobs_fun_get_good_comment_request_cid:(NSString*)cid
                                            page:(NSInteger)page
                                           handle:(void(^)(NSArray<THCommentModel*> * array))handle{

    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (page>-1) {
        [stoneobs_net_param_dic setObject:@(page) forKey:@"page"];
    }
    if (cid.length) {
        [stoneobs_net_param_dic setObject:cid forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/evaluates"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSArray * stoneobs_net_model_dic = [responseObject valueForKey:@"data"][@"items"];
        NSArray * dataSouce = [THCommentModel mj_objectArrayWithKeyValuesArray:stoneobs_net_model_dic];
        if (handle) {
            handle(dataSouce);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        if (handle) {
            handle(@[]);
        }
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
//收藏或者取消收藏
+ (void)stoneobs_fun_collect_good:(NSString*)good_id handle:(STTools_DataController_stoneobs_handle)handle{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (good_id.length) {
        [stoneobs_net_param_dic setObject:good_id forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/collection"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        if (handle) {
            handle(nil,YES,stoneobs_net_model_dic);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}

//添加购物车
+ (void)stoneobs_fun_add_car:(NSString*)good_id
                         num:(NSString*)num
                        type_name:(NSString*)type_name
                        spec_id:(NSString*)spec_id
                      handle:(STTools_DataController_stoneobs_handle)handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (good_id.length) {
        [stoneobs_net_param_dic setObject:good_id forKey:@"goods_id"];
    }
    if (num.length) {
        [stoneobs_net_param_dic setObject:num forKey:@"num"];
        if (num.intValue == 0) {
            [stoneobs_net_param_dic setObject:@"1" forKey:@"num"];
        }
    }else{
        [stoneobs_net_param_dic setObject:@"1" forKey:@"num"];
    }
    if (type_name.length) {
        [stoneobs_net_param_dic setObject:type_name forKey:@"type_name"];
    }
    if (spec_id.length) {
        [stoneobs_net_param_dic setObject:spec_id forKey:@"spec_id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/add"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"添加至购物车成功"];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        if (handle) {
            handle(nil,YES,stoneobs_net_model_dic);
        }
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
